<?php

declare(strict_types=1);

namespace app\controller\api;

use app\model\AdminUser;
use app\util\Sms;
use app\util\Tools;
use think\facade\Db;
use think\Response;
use app\util\WechatService;
use app\model\AdminUserData;

/**
 * 驾校平台端
 */
class JxAdmin extends Base
{

    //管理用用户登录

    public function login(): Response
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        if (!$username) {
            return $this->buildFailed(401, '缺少用户名');
        }
        if (!$password) {
            return $this->buildFailed(402, '缺少用户密码');
        } else {
            $password = Tools::userMd5($password);
        }
        $userInfo = (new AdminUser())->where('username', $username)->where('password', $password)->find();
        if (!empty($userInfo)) {
            if ($userInfo['status']) {
                //更新用户数据
                $userData = $userInfo->userData;
                $data = [];
                if ($userData) {
                    $userData->login_times++;
                    $userData->last_login_ip = sprintf("%u", ip2long($this->request->ip()));
                    $userData->last_login_time = time();
                    $userData->save();
                } else {
                    $data['login_times'] = 1;
                    $data['uid'] = $userInfo['id'];
                    $data['last_login_ip'] = sprintf("%u", ip2long($this->request->ip()));
                    $data['last_login_time'] = time();
                    $data['head_img'] = '';
                    AdminUserData::create($data);

                    $userInfo['userData'] = $data;
                }
            } else {
                return $this->buildFailed(403, '用户已经被封');
            }
        } else {
            return $this->buildFailed(403, '用户密码不正确');
        }


        return $this->buildSuccess($userInfo->toArray(), '登录成功');
    }


    /**
     * 修改自己的信息
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function updateAdmin()
    {
        $postData = $this->request->post();
        $admin_id = $postData['admin_id'];

        //用户密码
        if (isset($postData['password']) && $postData['password'] != '') {
            $password = Tools::userMd5($postData['password']);
            $map['password'] = $password;
        }
        //用户头像
        if (isset($postData['head_img']) && $postData['head_img'] != '') {
            $map['head_img'] = $postData['head_img'];
        }
        //用户昵称
        if (isset($postData['nickname']) && $postData['nickname'] != '') {
            $map['nickname'] = $postData['nickname'];
        }

        $postData['id'] = $admin_id;
        $res = AdminUser::update($postData);
        if ($res === false) {
            return $this->buildFailed(402, '修改失败');
        }
        return $this->buildSuccess();
    }
    //添加证件 证件管理
    public function addCertificate()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['certificate_name'] = $param['certificate_name'];


        if (isset($param['id']) && $param['id'] != '') {
            $res = Db::name('jx_certificate')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_certificate')->where('certificate_name', $param['certificate_name'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该证件名称已经添加');
            }

            $res = Db::name('jx_certificate')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }


    //证件列表
    public function certificateList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');

        $list = Db::table('jx_certificate')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_certificate')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //证件详情
    public function certificateDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_certificate')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除证件
    public function certificateDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_certificate')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }

    //用户办证大厅
    public function userCertificateList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where = [];
        if (isset($param['user_name']) && $param['user_name'] != '') {
            $where[] = ['user_name', 'like', '%' . $param['user_name'] . '%'];
        }
        if (isset($param['certificate_name']) && $param['certificate_name'] != '') {
            $where[] = ['ca.certificate_name', 'like', '%' . $param['certificate_name'] . '%'];
        }

        $where['c.status'] = array('in', '1,2');

        $list = Db::table('jx_user_certificate')->alias('c')->join('jx_certificate ca', 'ca.id=c.certificate_id')
            ->field('c.*,ca.certificate_name')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_user_certificate')->alias('c')->join('jx_certificate ca', 'ca.id=c.certificate_id')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }
    //添加或者修改轮播图
    public function addBanner()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['banner_title'] = $param['banner_title'];
        $map['banner_img'] = $param['banner_img'];
        $map['url'] = $param['url'];


        if (isset($param['id']) && $param['id'] != '') {
            $res = Db::name('jx_banners')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_banners')->where('banner_title', $param['banner_title'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            }
            $res = Db::name('jx_banners')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }


    //轮播图列表
    public function bannerList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['banner_title']) && $param['banner_title'] != '') {
            $where[] = ['banner_title', 'like', '%' . $param['banner_title'] . '%'];
        }

        $list = Db::table('jx_banners')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_banners')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //轮播图详情
    public function bannerDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_banners')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除轮播图
    public function bannerDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_banners')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }

    //学生审核列表
    public function studentAuthenticationList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where = [];
        if (isset($param['user_name']) && $param['user_name'] != '') {
            $where[] = ['user_name', 'like', '%' . $param['user_name'] . '%'];
        }
        if (isset($param['user_phone']) && $param['user_phone'] != '') {
            $where[] = ['user_phone', 'like', '%' . $param['user_phone'] . '%'];
        }
        $pass_status =  isset($param['pass_status']) && ($param['pass_status'] != '') ? $param['pass_status'] : 0; //0 未审核 1已审核 2驳回
        $where[] = ['pass_status', '=', $pass_status];
        $list = Db::table('jx_student_authentication')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_student_authentication')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //学生申请详情
    public function studentAuthenticationDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $id = isset($param['id']) && ($param['id'] != '') ? $param['id'] : 0;
        if ($id == 0) {
            return $this->buildFailed(402, '学生id不能为空');
        }

        $row = Db::table('jx_student_authentication')->where('id', $id)->find();
        if (!empty($row)) {
            $row['student_img'] = json_decode($row['student_img'], true);
        }
        $data['result'] = $row;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //学生申请通过或者驳回
    public function studentAuthenticationPass(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        $id = isset($param['id']) && ($param['id'] != '') ? $param['id'] : 0;
        $pass_type = isset($param['pass_type']) && ($param['pass_type'] != '') ? $param['pass_type'] : 0; // 1通过 2驳回

        if ($pass_type == 0) {
            return $this->buildFailed(402, '审核类别不能为空');
        };

        if ($id == 0) {
            return $this->buildFailed(402, '学生id不能为空');
        }
        $sainfo = Db::table('jx_student_authentication')->where('id', $id)->find();
        if ($pass_type == '1') {
            //学生审核成功
            $map['pass_status'] = 1;
            $map['update_time'] = date('Y-m-d H:i:s', time());
            $res = Db::table('jx_student_authentication')->where('id', $id)->save($map);
            $resA = Db::table('jx_users')->where('id', $id)->save(['is_student' => '1']);
            $noticeTitle = '学生认证审核成功';
        } else if ($pass_type == '2') {
            //学生认证拒绝
            $map['pass_status'] = 2;
            $map['update_time'] = date('Y-m-d H:i:s', time());
            if (isset($param['reject_remark']) && $param['reject_remark'] != '') {
                $map['reject_remark'] = $param['reject_remark'];
            }

            $res = Db::table('jx_student_authentication')->where('id', $id)->save($map);
            $resA = Db::table('jx_users')->where('id', $id)->save(['is_student' => '2']);
            $noticeTitle = '学生认证审核被拒绝';
        }
        $noticeArr['title'] = '学生认证审核';
        $noticeArr['user_id'] = $sainfo['user_id'];
        $noticeArr['content'] = $noticeTitle;
        $noticeArr['add_time'] = date('Y-m-d H:i:s', time());
        Db::table('jx_notice')->insert($noticeArr);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //平台设置教练服务协议
    public function setCoachAgreement(): Response
    {
        $param = $this->request->param();
        if (isset($param['coach_agreement']) && $param['coach_agreement'] == '') {
            return $this->buildFailed(400, '教练协议内容不能为空');
        }
        $map['content'] = $param['coach_agreement'];
        $row = Db::table('jx_system')->where('mark', 'coach_agreement')->save($map);
        return $this->buildSuccess();
    }
    //平台设置教练服务协议
    public function getCoachAgreement(): Response
    {
        $param = $this->request->param();

        $map['content'] = $param['coach_agreement'];
        $row = Db::table('jx_system')->where('mark', 'coach_agreement')->field('content')->find();
        return $this->buildSuccess($row);
    }

    //用户端想成为教练记录列表
    public function dreamCoachRecordList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where = [];

        if (isset($param['user_name']) && $param['user_name'] != '') {
            $where[] = ['user_name', 'like', '%' . $param['user_name'] . '%'];
        }


        if (isset($param['user_phone']) && $param['user_phone'] != '') {
            $where[] = ['user_phone', 'like', '%' . $param['user_phone'] . '%'];
        }

        $list = Db::table('jx_dream_coach_record')->field('r.*,u.nickname,u.user_img,u.user_phone')->alias('r')->join('jx_users u', 'u.id=r.user_id')->page((int)$page, (int)$limit)->where($where)->order('r.id desc')->select();
        $count = Db::table('jx_dream_coach_record')->alias('r')->join('jx_users u', 'u.id=r.user_id')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }





    //添加或者修改驾校
    public function addSchool()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['shcool_name'] = $param['shcool_name'];
        $map['school_image'] = $param['school_image'];
        $map['address'] = $param['address'];

        if (isset($param['id']) && $param['id'] != '') {
            $res = Db::name('jx_school')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_school')->where('shcool_name', $param['shcool_name'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            }
            $res = Db::name('jx_school')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }



    //驾校列表
    public function schoolList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['shcool_name']) && $param['shcool_name'] != '') {
            $where[] = ['shcool_name', 'like', '%' . $param['shcool_name'] . '%'];
        }

        $list = Db::table('jx_school')->page((int)$page, (int)$limit)->field('id,school_image,shcool_name,address,add_time')->where($where)->order('id desc')->select();
        $count = Db::table('jx_school')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //驾校详情
    public function schoolDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_school')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除驾校
    public function schoolDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_school')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }
    //添加或者修改平台公告
    public function addProclamation()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['title'] = $param['title'];
        $map['content'] = $param['content'];

        if (isset($param['id']) && $param['id'] != '') {
            $res = Db::name('jx_proclamation')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_proclamation')->where('title', $param['title'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            }
            $res = Db::name('jx_proclamation')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }

    //公告列表
    public function proclamationList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['title']) && $param['title'] != '') {
            $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        }

        $list = Db::table('jx_proclamation')->page((int)$page, (int)$limit)->where($where)->order('id desc')->select();
        $count = Db::table('jx_proclamation')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //公告详情
    public function proclamationDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_proclamation')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除公告
    public function proclamationDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::table('jx_proclamation')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }



    //添加或者修改驾校场地
    public function addSchoolState()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['state_name'] = $param['state_name'];
        $map['state_image'] = $param['state_image'];
        $map['address'] = $param['address'];

        if (isset($param['id']) && $param['id'] != '') {
            $res = Db::name('jx_school_state')->where('id', $param['id'])->save($map);
        } else {
            //添加
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_school_state')->where('state_name', $param['state_name'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            }
            $res = Db::name('jx_school_state')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }



    //驾校场地列表
    public function schoolStateList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['state_name']) && $param['state_name'] != '') {
            $where[] = ['state_name', 'like', '%' . $param['state_name'] . '%'];
        }

        $list = Db::table('jx_school_state')->page((int)$page, (int)$limit)->field('id,state_image,state_name,address,add_time')->where($where)->order('id desc')->select();
        $count = Db::table('jx_school_state')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //驾校场地详情
    public function schoolStateDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_school_state')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除驾校场地
    public function schoolStateDel(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_school_state')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }

    //教练未审列表
    public function noPassCoachList(): Response
    {

        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;

        $pass_status =  isset($param['pass_status']) && ($param['pass_status'] != '') ? $param['pass_status'] : 0;  //0未审核 1已审核  2驳回
        //驾考类型
        if (isset($param['jiakao_type']) && $param['jiakao_type'] != '') {
            $where[] = ['co.jiakao_type', '=', $param['jiakao_type']];
        }
        //教练姓名
        if (isset($param['user_name']) && $param['user_name'] != '') {
            $where[] = ['co.user_name', '=', $param['user_name']];
        }

        //教练电话
        if (isset($param['user_phone']) && $param['user_phone'] != '') {
            $where[] = ['co.user_phone', 'like', $param['user_phone'] . '%'];
        }

        $where[] = ['co.pass_status', '=', $pass_status];
        $field = 'co.id,user_name,Id_card,user_phone,jiakao_type,co.add_time,school_id,s.shcool_name';
        $list = Db::table('jx_coach')->alias('co')->join('jx_school s', 's.id=co.school_id')->page((int)$page, (int)$limit)->field($field)->where($where)->order('id desc')->select();
        $count = Db::table('jx_coach')->alias('co')->join('jx_school s', 's.id=co.school_id')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        return $this->buildSuccess($data);
    }

    /**
     * 教练详情
     */
    public function CoachDetail(): Response
    {

        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $id = $param['id'];  //教练用户id
        $where[] = ['co.id', '=', $id];
        //  $where[] = ['co.is_perfect', '=', '1'];
        $field = 'co.*,s.shcool_name,st.state_name';
        $row = Db::table('jx_coach')->alias('co')
            ->join('jx_school s', 's.id=co.school_id')
            ->join('jx_school_state st', 'st.id=co.train_address')
            ->where($where)
            ->field($field)->order('id desc')->find();
        if ($row) {
            return $this->buildSuccess($row);
        } else {
            return $this->buildFailed(401, '无数据');
        }
    }

    /**
     * 通过或者驳回教练审核
     */
    public function setCoachStatus(): Response
    {

        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['reject_remark']) && $param['reject_remark'] != '') {
            $map['reject_remark'] = $param['reject_remark'];
        }
        $pass_status = isset($param['pass_status']) && ($param['pass_status'] != '') ? $param['pass_status'] : 1;
        $map['pass_status'] = $pass_status;
        $map['reject_time'] = date('Y-m-d H:i:s', time());
        $id = $param['id'];  //教练用户id
        $res = Db::table('jx_coach')->where('id', $id)->save($map);
        return $this->buildSuccess();
    }

    /**
     * 设置电话客服
     */
    public function setPhoneCustomer(): Response
    {

        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['phone_customer']) && $param['phone_customer'] != '') {
            $map['content'] = $param['phone_customer'];
        } else {
            return $this->buildFailed(401, '电话客服不能为空');
        }

        $res = Db::table('jx_system')->where('mark', 'phone_customer')->save($map);
        return $this->buildSuccess();
    }

    /**
     * 获取电话客服
     */
    public function getPhoneCustomer(): Response
    {
        $res = Db::table('jx_system')->where('mark', 'phone_customer')->find();
        return $this->buildSuccess($res);
    }
    /**
     * 设置代理返佣比例
     */
    public function setSystemRebate(): Response
    {

        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        //代理晋升人数
        if (isset($param['agency_get_num']) && $param['agency_get_num'] != '') {
            $map['agency_get_num'] = $param['agency_get_num'];
        }
        //普通用户返佣
        if (isset($param['general_user_rebate']) && $param['general_user_rebate'] != '') {
            $map['general_user_rebate'] = $param['general_user_rebate'];
        }


        //平级奖励机制
        if (isset($param['equal_level_reward']) && $param['equal_level_reward'] != '') {
            $map['equal_level_reward'] = $param['equal_level_reward'];
        }
        if (!empty($map)) {
            foreach ($map as $k => $v) {
                $res = Db::table('jx_system')->where('mark', $k)->save(['content' => $v]);
            }
        } else {
            return $this->buildFailed(401, '参数错误');
        }

        return $this->buildSuccess();
    }

    /**
     * 获取电话客服
     */
    public function getSystemRebate(): Response
    {
        $res = Db::table('jx_system')->where('mark', 'general_user_rebate')->find();
        $general_user_rebate = $res['content'];

        $res4 = Db::table('jx_system')->where('mark', 'agency_get_num')->find();
        $agency_get_num = $res4['content'];

        $res5 = Db::table('jx_system')->where('mark', 'equal_level_reward')->find();
        $equal_level_reward = $res5['content'];

        $data['general_user_rebate'] = $general_user_rebate;
        $data['agency_get_num'] = $agency_get_num;
        $data['equal_level_reward'] = $equal_level_reward;
        return $this->buildSuccess($data);
    }

    //切换教练
    public function changeCoachAmount(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $data = Db::name('jx_change_coach_amount')->column('changeTypeName,changeAmount');
        return $this->buildSuccess($data);
    }
    //设置切换教练费用
    public function setChangeCoachAmount(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['change_json']) && $param['change_json'] == '') {
            return $this->buildFailed(400, '设置修改的json串不能为空');
        }
        $jsonStr = html_entity_decode($param['change_json']);
        $jsonArr = json_decode($jsonStr, true);
        foreach ($jsonArr as $k => $v) {
            $where['changeTypeName'] = $k;
            $umap['add_time'] = date('Y-m-d H:i:s', time());
            $umap['changeAmount'] = $v;
            $res = Db::name('jx_change_coach_amount')->where($where)->update($umap);
        }
        return $this->buildSuccess();
    }
}
